www.gusucode.com > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序 > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序\code\PhManage\HospitalDlg.cpp
//Download by http://www.NewXing.com // HospitalDlg.cpp : implementation file // #include "stdafx.h" #include "PhysicM.h" #include "HospitalDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CHospitalDlg dialog CHospitalDlg::CHospitalDlg(CWnd* pParent /*=NULL*/) : CDialog(CHospitalDlg::IDD, pParent) { //{{AFX_DATA_INIT(CHospitalDlg) m_demo = _T(""); m_jc = _T(""); m_name = _T(""); //}}AFX_DATA_INIT } void CHospitalDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CHospitalDlg) DDX_Control(pDX, IDC_HOSPITAL_LIST, m_List); DDX_Text(pDX, IDC_DEMO_EDIT, m_demo); DDX_Text(pDX, IDC_HOSPITAL_JC, m_jc); DDX_Text(pDX, IDC_HOSPITAL_NAME, m_name); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CHospitalDlg, CDialog) //{{AFX_MSG_MAP(CHospitalDlg) ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton) ON_BN_CLICKED(IDC_DELETE_BUTTON, OnDeleteButton) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CHospitalDlg message handlers void CHospitalDlg::OnOK() { // TODO: Add extra validation here // CDialog::OnOK(); } void CHospitalDlg::OnCancel() { // TODO: Add extra cleanup here CDialog::OnCancel(); } BOOL CHospitalDlg::OnInitDialog() { CDialog::OnInitDialog(); m_List.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT); char *head[]={"序 号","医院名称","医院简称","医院ID","备注"}; int i; for (i=0;i<5;i++) m_List.InsertColumn(i,head[i],LVCFMT_LEFT,(i==1) ? 150 : 100,i); ImageList=new CImageList(); ImageList->Create(16,16,TRUE | ILC_COLOR32,2,0); ImageList->Add(AfxGetApp()->LoadIcon(IDI_ICONUSER)); ImageList->Add(AfxGetApp()->LoadIcon(IDR_MENUVIEW_TMPL)); m_List.SetImageList(ImageList,LVSIL_SMALL);// LVSIL_NORMAL); Init(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CHospitalDlg::Init() { DBLibrary DB(((CPhysicMApp *)AfxGetApp())->DBSession); try{ DB.Open("select hospital_id hid,hospital_name hn,hospital_jc hj,demo dm from hospital order by hospital_id"); int id=0,no=0;; char tmp[50]; memset(tmp,0,50); while(!DB.isEof()) { sprintf(tmp,"%03d",no+1); m_List.InsertItem(no,tmp,0); DB.GetValue("hn",tmp); m_List.SetItemText(no,1,(LPCTSTR)tmp); DB.GetValue("hj",tmp); m_List.SetItemText(no,2,(LPCTSTR)tmp); id=DB.GetValue("hid"); sprintf(tmp,"%03d",id); m_List.SetItemText(no,3,(LPCTSTR)tmp); memset(tmp,0,50); DB.GetValue("dm",tmp); m_List.SetItemText(no,4,(LPCTSTR)tmp); no++; DB.Next(); } }catch (DBErr &err) { char *str; int code; err.GetLastErr(code,&str); ((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("%s(%d)",str,code); } catch (...) { ((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("unknow err(%d)",-1); } } void CHospitalDlg::AddInfo(CString m_pname,CString m_pj,CString m_dm) { DBLibrary DB(((CPhysicMApp *)AfxGetApp())->DBSession); int max_id=0; try{ DB.Open("select IDENT_CURRENT('hospital') did " " from INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='hospital' "); double id=0; DB.GetValue("did",&id); max_id=(int)id+1; DB.ExecSQL("INSERT INTO hospital (hospital_name,hospital_jc,demo) " "VALUES('%s','%s','%s')",m_pname,m_pj,m_dm); ((CPhysicMApp *)AfxGetApp())->pMainFrm->AddLog("新增医院:%s",m_pname); char no[4]; int icount=m_List.GetItemCount(); memset(no,0,4); sprintf(no,"%03d",icount+1); m_List.InsertItem(m_List.GetItemCount(),no,0); m_List.SetItemText(icount,1,(LPCTSTR)m_pname); m_List.SetItemText(icount,2,(LPCTSTR)m_pj); sprintf(no,"%03d",max_id); m_List.SetItemText(icount,3,(LPCTSTR)no); m_List.SetItemText(icount,4,(LPCTSTR)m_dm); } catch (DBErr &err) { char *str; int code; err.GetLastErr(code,&str); ((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("%s(%d)",str,code); } catch(...) { ((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("unknow err(%d)",-1); } } BOOL CHospitalDlg::DestroyWindow() { // TODO: Add your specialized code here and/or call the base class if (ImageList) delete ImageList; return CDialog::DestroyWindow(); } void CHospitalDlg::OnAddButton() { UpdateData(); m_name.TrimLeft(); m_name.TrimRight(); if(m_name.IsEmpty()) { ((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("医院名称不能为空,请输入"); return; } AddInfo(m_name,m_jc,m_demo); } void CHospitalDlg::OnDeleteButton() { ((CPhysicMApp *)AfxGetApp())->Delete("Hospital","Hospital_id",&m_List,3); }